package com.trailwalker.traceability.net;

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import com.trailwalker.traceability.util.GsonUtils;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


public class Network {
    private static final String TAG = Network.class.getSimpleName();
    private static OkHttpClient okHttpClientInstance;
    private static OkHttpClient okHttpClientNormalInstance;
    private static final boolean DEBUG = true;

    private static Map<String, Retrofit> retrofitMap = new HashMap<>();

    public static OkHttpClient getNormalOkHttpClientInstance() {
        if (okHttpClientNormalInstance == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS);
            okHttpClientNormalInstance = builder.build();
        }
        return okHttpClientNormalInstance;
    }

    public static OkHttpClient getOkHttpClientInstance(List<Interceptor> interceptorList) {
        if (okHttpClientInstance == null) {
            HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                @Override
                public void log(String message) {
//                    System.err.println(TAG + " : " + message);
                }
            });
            httpLoggingInterceptor.setLevel(DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
            //网络缓存路径文件
            // File httpCacheDirectory = new File(BaseApplication.getInstance().getExternalCacheDir(), "responses");

            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS);
            if (interceptorList != null && interceptorList.size() > 0) {
                for (Interceptor interceptor : interceptorList) {
                    builder.addInterceptor(interceptor);
                }
            }
            okHttpClientInstance = builder
                    .addInterceptor(httpLoggingInterceptor)
                    .addNetworkInterceptor(httpLoggingInterceptor)
                    .hostnameVerifier((s, sslSession) -> true)
                    .build();
            //build.cache(new Cache(getCacheFile(context), CACHE_SIZE));
        }
        return okHttpClientInstance;
    }

    public static Retrofit getRetrofitInstance(String baseUrl) {
        if (retrofitMap.containsKey(baseUrl)) {
            return retrofitMap.get(baseUrl);
        }
        return getRetrofitInstance(baseUrl, Collections.emptyList());
    }

    public static Retrofit getRetrofitInstance(String baseUrl, List<Interceptor> interceptorList) {
        if (retrofitMap.containsKey(baseUrl)) {
            return retrofitMap.get(baseUrl);
        }
        return new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(getOkHttpClientInstance(interceptorList))
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(GsonUtils.getGson()))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }


}
